package com.bysj.agent;



import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.web.search.WebSearchTool;
import dev.langchain4j.web.search.searchapi.SearchApiWebSearchEngine;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class test {

    interface Assistant {
        @SystemMessage({
                "您是一个网络搜索支持代理。",
                "如果有任何尚未发生的事件，",
                "您必须创建一个带有用户查询的网络搜索请求，并",
                "使用网络搜索工具搜索网络上的有机网页结果。",
                "在您的最终回复中包含来源链接。"
        })
        String answer(String userMessage);
    }
    @Test
    public void webTest(){
        Map<String, Object> optionalParameters = new HashMap<>();
        optionalParameters.put("gl", "us");
        optionalParameters.put("hl", "en");
        optionalParameters.put("google_domain", "google.com");

        SearchApiWebSearchEngine searchEngine = SearchApiWebSearchEngine.builder()
                .apiKey("juPxNsHSXDSSBN2ovbJtfr34")
                .engine("google")
                .optionalParameters(optionalParameters)
                .build();
        ChatLanguageModel chatModel = OpenAiChatModel.builder()
                .apiKey("sk-a691c2b981cc401e99536eeca9ba7af4")
                .modelName("qwen-max")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)
                .build();

        WebSearchTool webTool = WebSearchTool.from(searchEngine);

        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(chatModel)
                .tools(webTool)
                .build();

        String answer = assistant.answer("我想知道今天兰州都发生了什么事情，并给出日期和来源");
        System.out.println(answer);
    }
}
